#region Import Declarations

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using CommunityServer.Components;
using System.Web.UI.WebControls;
using CommunityServer.Controls;
using System.Reflection;
using CommunityServer;
using Telligent.Glow;

#endregion

namespace CommunityServerStuff.Controls
{
    public class ProfileSubForm : WrappedSubFormBase
    {

        #region Private Members

        private List<string> _profileProperties = null;

        #endregion

        #region Properties

        #region IsEnabled

        public override bool IsEnabled()
        {
            return true;
        }

        #endregion

        #region DataSource

        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (value is User || value == null)
                    base.DataSource = value;
                else
                    throw new InvalidCastException("DataSource must be a valid User object");
            }
        }

        #endregion

        #region ProfileProperties

        public string ProfileProperties
        {
            get { return ((string)ViewState["ProfileProperties"]) ?? string.Empty; }
            set
            {
                ViewState["ProfileProperties"] = value;
                if (!string.IsNullOrEmpty(value))
                {
                    _profileProperties = new List<string>();
                    foreach (string ea in value.Split(','))
                    {
                        if (!string.IsNullOrEmpty(ea.Trim()))
                            _profileProperties.Add(ea.Trim());
                    }
                }
            }
        }

        #endregion

        #endregion

        #region Methods

        #region AttachChildControls

        protected override void AttachChildControls()
        {
            //Not implemented
        }

        #endregion

        #region ApplyChangesAfterCommit

        public override void ApplyChangesAfterCommit(object activeObject)
        {
            if (_profileProperties == null)
                return;

            EnsureChildControls();

            if (!this.IsValid())
                return;

            User u = activeObject as User;
            if (u == null)
                return;

            Type type = u.Profile.GetType();
            foreach (string controlName in _profileProperties)
            {
                Control c = CSControlUtility.Instance().FindControl(this, controlName);
                if (c != null)
                {
                    string value = null;

                    if (c is TextBox)
                        value = ((TextBox)c).Text.Trim();
                    else if (c is CheckBox)
                        value = ((CheckBox)c).Checked.ToString();
                    else if (c is ListControl)
                        value = ((ListControl)c).SelectedValue;
                    else if (c is DateTimeSelector)
                        value = ((DateTimeSelector)c).DateTime.ToString();

                    if (value != null)
                    {
                        try
                        {
                            PropertyInfo senderProperty = type.GetProperty(c.ID);
                            switch (c.ID)
                            {
                                case "BirthDate":
                                    senderProperty.SetValue(u.Profile, DateTime.Parse(value), null);
                                    break;

                                case "EnablePostPreviewPopup":
                                case "EnableEmoticons":
                                    senderProperty.SetValue(u.Profile, bool.Parse(value), null);
                                    break;

                                case "Gender":
                                    senderProperty.SetValue(u.Profile, System.Enum.Parse(typeof(Gender), value, false), null);
                                    break;

                                default:
                                    senderProperty.SetValue(u.Profile, Globals.HtmlEncode(value), null);
                                    break;
                            }

                            string message = String.Format("Profile Data Added: {0} = {1}.", senderProperty.Name, value);
                            EventLogs.Warn(message, "Registration", 301);
                        }
                        catch (Exception ex)
                        {
                            CSException csex = new CSException(CSExceptionType.UnknownError, null, ex);
                            csex.Log(CSContext.Current.SettingsID);
                        }
                    }
                }
            }
            Users.UpdateUser(u);
        }

        #endregion

        #endregion

    }
}